//
//  ClaBgCollectionView.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2024/11/5.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
import JJCollectionViewRoundFlowLayout_Swift

class ClaBgCollectionView: UIView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
   
    
   
    var dataArray = [String]()
    var maxlengths = [Int]()
 
 
    lazy var collectionView:UICollectionView = {
        // 1.创建layout
        let sectionW = self.ks_width - 24
        let layout = JJCollectionViewRoundFlowLayout_Swift()
        layout.itemSize = CGSize(width: 80, height: 35)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        // 2.创建UICollectionView
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.register(ChatTableInnerCell.self, forCellWithReuseIdentifier: ChatTableInnerCell.identifier)
//        collectionView.register(FunctionCenterHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: FunctionCenterHeader.identifier)
       
        return collectionView
    }()
   
    var page = 1
    var lastinfodic = [[String:Any]]()
    var topH:CGFloat!
    var selectItemClo:((IndexPath)->())?
     override init(frame: CGRect) {
        super.init(frame: frame)
 
        self.backgroundColor = .clear
         collectionView.backgroundColor = .clear
        
    
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.frame = self.bounds
        self.addSubview(collectionView)
    
         
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        
        let ww = indexPath.row
  
        return CGSize(width: maxlengths[ww], height: 35)
    }
    
  
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
   
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = self.dataArray[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChatTableInnerCell.identifier, for: indexPath as IndexPath) as! ChatTableInnerCell
        KSLog("\(indexPath.row)---\(model)")
        cell.setContent(contentStr: model)
         return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let clo = selectItemClo {
            clo(indexPath)
        }
    }
}
extension ClaBgCollectionView : JJCollectionViewDelegateRoundFlowLayout_Swift{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, borderEdgeInsertsForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 12, left: 12, bottom: 12, right: 12)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, configModelForSectionAtIndex section: Int) -> JJCollectionViewRoundConfigModel_Swift {
        let model = JJCollectionViewRoundConfigModel_Swift.init()
        model.cornerRadius = 8
        model.backgroundColor = .clear
        
        
        return model;
        
    }
}
class ChatTableInnerCell: BadgeCollectionViewCell {
    
    private lazy var content:UILabel = {
         let v = UILabel()
         v.textAlignment = .center
         v.font = UIFont.systemFont(ofSize: 15)
         return v
     }()
     private lazy var slash:UILabel = {
         let v = UILabel()
         v.text = "/"
         v.font = UIFont.systemFont(ofSize: 15)
         return v
     }()
     
     func setContent(contentStr:String) {
         content.text = contentStr
         if contentStr == "全部" {
             content.textColor = .black
             slash.textColor = .black
             content.font = UIFont.boldSystemFont(ofSize: 17)
             slash.font = UIFont.boldSystemFont(ofSize: 17)

         } else {
             content.textColor = Theme.primaryColor()
             slash.textColor = Theme.primaryColor()
             content.font = UIFont.boldSystemFont(ofSize: 15)
             slash.font = UIFont.boldSystemFont(ofSize: 15)
             
         }
     }
  
   
    
    
    convenience required init(coder : NSCoder){
        self.init()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUtil()
    }
    
    func initUtil() {
        self.contentView.backgroundColor = .clear
        self.addSubview(slash)
        self.addSubview(content)
        slash.isUserInteractionEnabled = false
        
        slash.snp.makeConstraints { make in
            make.right.equalTo(content.snp.left).offset(-9)
            make.centerY.equalTo(self)
        }
      
        content.isUserInteractionEnabled = false
        content.snp.makeConstraints { make in
            make.centerX.equalTo(self).offset(6)
            make.centerY.equalTo(self)

        }

        
    }
    
    
    
  
}
